// pages/address_administration/address_administration.js
const app = getApp()
const Host = require('../../config').Host
Page({

    /**
     * 页面的初始数据
     */
    data: {
        defaultIndex:-1,
        deleteIndex:-1,
        addressList:[],
    },
    //微信获取收货地址
    chooseAddress:function(){
        var that = this
        var openid = wx.getStorageSync('openid')
        if (wx.chooseAddress) {
            wx.chooseAddress({
                success: function (res) {
                  //console.log(JSON.stringify(res))
                  wx.request({
                    url: Host + 'Order/address_wx',
                    data: { "openid": openid, "wxaddr": JSON.stringify(res)},
                    method: "post",
                    header: { 'content-type': 'application/x-www-form-urlencoded' },
                    success: function (res2) {
                      if (res2.data.code==200) {
                        that.onLoad()
                      } else {
                        wx.showToast({
                          title: res2.data.errmsg,
                          icon: 'none',
                        })
                      }
                    }
                  })
                    // var addressList = that.data.addressList;
                    // addressList.forEach(item=>{
                    //     item.isDefault=false;
                    // })
                    // var data={
                    //     id: res.nationalCode,
                    //     name:res.userName,
                    //     phone:res.telNumber,
                    //     address: res.provinceName+res.cityName+res.countyName+res.detailInfo,
                    //     isDefault:true,
                    // };
                    // addressList.unshift(data);
                    // that.setData({
                    //     addressList: addressList,
                    // })
                },
                fail: function (err) {
                    console.log(JSON.stringify(err))
                }
            })
        } else {
            console.log('当前微信版本不支持chooseAddress');
        }
    },
    //返回下单页
  backpage:function(e){
    var id = e.currentTarget.id
    //console.log(id)
    wx.setStorageSync('addressId', id)
    wx.navigateTo({
      url: '/pages/order/order',
    })
  },
    //选择默认地址
    chooseDefaultIndex:function(e){
      var index=e.currentTarget.id;
      var that = this;
      var addressList = that.data.addressList;
      var id = addressList[index].id;
      var openid = wx.getStorageSync('openid');
      //console.log(id)//地址的id
      wx.request({
        url: Host + 'Order/address_defult',
        data: { id, openid },
        success: function (res) {
          if (res.data.code == 200) {
            if (addressList[index].isDefault == false) {
              addressList.forEach(item => {
                item.isDefault = false;
              })
              addressList[index].isDefault = true;
            } else {
              addressList[index].isDefault = false;
            }
            that.setData({
              defaultIndex: index,
              addressList: addressList,
            })
          } else {
            wx.showToast({
              title: res.data.errmsg,
              icon: 'none',
            })
          }

        }
      })
    },
    //删除
    delete1:function(e){
        var index=e.currentTarget.id;
        var that=this;
        that.setData({
            deleteIndex:index,
        })
        var addressList = that.data.addressList;
        wx.showModal({
            title: '',
            content: '确定要删除吗?',
            showCancel: true,
            success: function (res) {
                if (res.cancel) {
                    that.setData({
                        deleteIndex:-1,
                    })
                } else {
                    var id = addressList[that.data.deleteIndex].id;
                    var openid = wx.getStorageSync('openid');
                    //console.log(id)//删除商品的id
                    wx.request({
                      url: Host + 'Order/address_del',
                      data: { id, openid},
                      success: function (res) {
                        if (res.data.code==200) {
                          addressList.splice(that.data.deleteIndex, 1);
                          that.setData({
                            addressList: addressList,
                          })
                        } else {
                          wx.showToast({
                            title: res.data.errmsg,
                            icon: 'none',
                          })
                        }
                        
                      }
                    })
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      var that = this
      var openid = wx.getStorageSync('openid')
      //获取地址列表
      wx.request({
        url: Host + 'Order/address_list',
        data: { "openid": openid },
        method: "post",
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
          that.setData({
            addressList: res.data.list
          })
        }
      })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})